package com.tjn.generic;

import java.util.ArrayList;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class Generic02 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        //创建一个ArrayList对象,加入泛型<Dog2>
        ArrayList<Dog2> arrayList = new ArrayList<Dog2>();
        arrayList.add(new Dog2("旺财",1));
        arrayList.add(new Dog2("发财",2));
        arrayList.add(new Dog2("小黄",3));

        //arrayList.add(new Cat2("招财",4));
        //泛型的好处：
        //1.加入泛型<Dog2>，在这里添加cat对象之后，在遍历的时候，就会报错，这样就可以检查添加元素的类型
        //2.这样加入泛型之后，就不用强转类型，提高效率
        for (Dog2 o : arrayList) {
            System.out.println(o.getName() + " " + o.getAge());
        }
    }
}
class Dog2{
    private String name;
    private int age;

    public Dog2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat2{
    private String name;
    private int age;
    public Cat2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
}